package com.prototype.ECE492;

//import java.util.List;

//import com.google.android.maps.Overlay;
//import com.prototype.ECE492.R.drawable;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class Menu_Activity extends Activity {
	//ROYGB
//	LocationManager locationManager;
	boolean blue_var;
	boolean red_var;
	boolean orange_var;
	boolean green_var;
	boolean yellow_var;
	boolean gps_status;
	CheckBox blue;
	CheckBox red;
	CheckBox orange;
	CheckBox yellow;
	CheckBox green;	
	ToggleButton toggle;	
	SharedPreferences menuSettings;
	SharedPreferences.Editor prefEditor;
	Spinner spinner;
	ArrayAdapter<CharSequence> adapter;
	ViewGroup views;
	@Override
	public void onCreate(Bundle savedInstancestate){
		super.onCreate(savedInstancestate);
//		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		menuSettings = getSharedPreferences("MenuPref", MODE_PRIVATE);
		prefEditor = menuSettings.edit();
		if(menuSettings.getInt("Blue", 0) == 1)blue_var = true;else blue_var = false;
		if(menuSettings.getInt("Orange", 0) == 1)orange_var = true;else orange_var = false;
		if(menuSettings.getInt("Red", 0) == 1)red_var = true;else red_var = false;
		if(menuSettings.getInt("Yellow", 0) == 1)yellow_var = true;else yellow_var = false;
		if(menuSettings.getInt("Green", 0) == 1)green_var = true;else green_var = false;
		if(menuSettings.getInt("GPS", 1) == 1)gps_status = true;else gps_status = false;
		setContentView(R.layout.menu);
		Button back = (Button)findViewById(R.id.Button02);
//		CharSequence text = new String("BlueChange");
		blue = (CheckBox)findViewById(R.id.checkBox5);
//		blue.setText(text);  //this was to test if I could do something
		red = (CheckBox)findViewById(R.id.checkBox1);
		orange = (CheckBox)findViewById(R.id.checkBox2);
		green = (CheckBox)findViewById(R.id.checkBox4);
		yellow = (CheckBox)findViewById(R.id.checkBox3);
		blue.setChecked(blue_var);
		red.setChecked(red_var);
		orange.setChecked(orange_var);
		green.setChecked(green_var);
		yellow.setChecked(yellow_var);
		toggle = (ToggleButton)findViewById(R.id.toggleButton1);
		toggle.setChecked(gps_status);
		views = (LinearLayout) findViewById(R.id.button_layout);
		views.addView(new CheckBox(this));
		//Set click listeners
		toggle.setOnClickListener(new View.OnClickListener(){
			public void onClick(View arg0){
				gps_status = !gps_status;
				if(gps_status){
					prefEditor.putInt("GPS", 1);
				}
				else{
					prefEditor.putInt("GPS",0);
				}
				prefEditor.commit();
			}
		});
		back.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				//onPause();
				finish();
			}
		});
		red.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				red_var = !red_var;
				if(red_var){
					prefEditor.putInt("Red", 1);
				}
				else{ 
					prefEditor.putInt("Red", 0);
				}
				prefEditor.commit();
			}
		});
		orange.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				orange_var = !orange_var;
				if(orange_var)prefEditor.putInt("Orange", 1);else prefEditor.putInt("Orange", 0);
				prefEditor.commit();

			}
		});
		yellow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				yellow_var = !yellow_var;
				if(yellow_var)prefEditor.putInt("Yellow", 1);else prefEditor.putInt("Yellow", 0);
				prefEditor.commit();

			}
		});
		green.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				green_var = !green_var;
				if(green_var){
					prefEditor.putInt("Green",1);
				}
				else{
					prefEditor.putInt("Green",0);
				}
				prefEditor.commit();

			}
		});
		blue.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				blue_var = !blue_var;
				if(blue_var)prefEditor.putInt("Blue", 1);else prefEditor.putInt("Blue", 0);
				prefEditor.commit();
			}
		});
		spinner = (Spinner) findViewById(R.id.spinner1);
		adapter = ArrayAdapter.createFromResource(
				this, R.array.color_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(menuSettings.getInt("COLOR_CHOICE", 3));
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		/*
     Red 0
     Orange 1
     Yellow 2
     Default:Green 3
     Blue 4
		 */

	}
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent,
				View view, int pos, long id) {
			prefEditor.putInt("COLOR_CHOICE", pos);
			prefEditor.commit();
		}
		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}	

}
